package com.example.tourpal.util;

import android.content.Context;
import android.content.SharedPreferences;

public class SharePreferencesManager {
	private static final String TAG = "chengongguo/SharePreferencesManager";
	private SharedPreferences userInfo;
	Tools mTools;

	public SharePreferencesManager(Context context) {
		mTools = new Tools(context, TAG);
		userInfo = context.getSharedPreferences("user_info", 0);
	}

	public class LoginInfo {
		private Boolean mAutoLogin;
		private String mEmail;
		private String mPassword;

		public LoginInfo() {
		}

		public LoginInfo(boolean autoLogin, String email, String password) {
			this.mAutoLogin = autoLogin;
			this.mEmail = email;
			this.mPassword = password;
		}
		public boolean getAutoLogin(){
			return mAutoLogin;
		}
		public String getEmail(){
			return mEmail;
		}
		public String getPassword(){
			return mPassword;
		}
	}

	public LoginInfo readUserInfo() {
		LoginInfo user = new LoginInfo();
		user.mAutoLogin = userInfo.getBoolean("user_auto_login", false);
		user.mEmail = userInfo.getString("user_email", "");
		user.mPassword = userInfo.getString("user_password", "");
		return user;
	}

	public void writeUserInfo(boolean autoLogin, String email, String password) {
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putBoolean("user_auto_login", autoLogin);
		editor.putString("user_email", email);
		editor.putString("user_password", password);
		editor.commit();
	}

	public void writeUserAccessToken(String accessToken){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_accessToken", accessToken);
		editor.commit();
	}
	
	public String readUserAccessToken(){
		return userInfo.getString("user_accessToken", null);
	}
	
	public void writeUserId(int id){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putInt("user_id", id);
		editor.commit();
	}
	
	public int readUserId(){
		return userInfo.getInt("user_id", 0);
	}

	public void writeUserName(String name){
		SharedPreferences.Editor editor = userInfo.edit();
		editor.putString("user_name", name);
		editor.commit();
	}
	
	public String readUserName(){
		return userInfo.getString("user_name", null);
	}
}
